/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.io.Serializable;
import java.sql.Date;
import java.util.*;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.dao.DanhGiaDAO;
import model.dao.PhongTroDAO;
import model.dao.QuanDAO;
import model.dao.UserDAO;
import model.entity.Danhgia;
import model.entity.PhongTro;
import model.entity.Quan;
import model.entity.User;

/**
 *
 * @author duy
 */
//@ManagedBean(name = "phongtroBean", eager = true)
@ManagedBean
@RequestScoped
public class PhongBean {

    private Danhgia danhgia;
    private PhongTro phongTro;
    private String searchbyUser;
    private final HttpServletRequest request;
    private final FacesContext fc;
    private final HttpSession session;
    private FacesMessage facesMessage;
    private final Map<String, String> params;

    PhongTroDAO PHONGTRO_SERVICE = PhongTroDAO.getInstance();
    QuanDAO QUAN_SERVICE = QuanDAO.getInstance();
    UserDAO USER_SERVICE = UserDAO.getInstance();
    DanhGiaDAO DANHGIA_SERVICE = DanhGiaDAO.getInstance();

    public PhongBean() {
        fc = FacesContext.getCurrentInstance();
        request = (HttpServletRequest) fc.getExternalContext().getRequest();
        params = fc.getExternalContext().getRequestParameterMap();
        session = request.getSession(true);
        this.phongTro = new PhongTro();
        this.danhgia = new Danhgia();
    }
    private List<PhongTro> allPhongTro;

    public List<PhongTro> getAllPhongTro() {
        List<PhongTro> allPhongTro = PHONGTRO_SERVICE.getAllPhong();
        return allPhongTro;
    }

    public List<Quan> getAllQuan() {

        return QUAN_SERVICE.getAllQuan();
    }

    public void setAllQuan(List<Quan> allQuan) {
        this.allQuan = allQuan;
    }
    private List<Quan> allQuan;
    private List<User> allUser;

    public List<User> getAllUser() {
        List<User> userList = USER_SERVICE.getAllUser();
        return userList;
    }

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private Quan quan;

    public PhongTro getPhongTro() {
        return phongTro;
    }

    public void setPhongTro(PhongTro phongTro) {
        this.phongTro = phongTro;
    }

    public Danhgia getDanhgia() {
        return danhgia;
    }

    public void setDanhgia(Danhgia danhgia) {
        this.danhgia = danhgia;
    }

    public Quan getQuan() {
        return quan;
    }

    public void setQuan(Quan quan) {
        this.quan = quan;
    }

    public String edit(PhongTro phongtro1) {
        this.phongTro = phongtro1;
        return "sua.jsf ";
    }

    public void xoa(int madanhgia) {
        FacesMessage mess;
        try {
            if (PHONGTRO_SERVICE.xoa(madanhgia)) {
                mess = new FacesMessage("Success!");
            } else {
                mess = new FacesMessage("fail!");
            }
            fc.addMessage("result", mess);

        } catch (Exception ex) {

        }
    }

    public String test() {
        return "/module/danhsach.jsf?faces-redirect=true";
    }

    public String createPhongTro() {
// Tao moi Danh Gia
        int maphongtro = getDanhgia().getMadanhgia();
        float diemdanhgia = getDanhgia().getDiemdanhgia();
        int songuoidanhgia = getDanhgia().getSonguoidanhgia();
        String diachiMAC = getDanhgia().getDiachiMAC();

        Danhgia danhgia = new Danhgia(diemdanhgia, 0, "1");
// Tao moi phong tro 
        String tieudegioithieu = getPhongTro().getTieudegioithieu();
        String tenchunha = getPhongTro().getTenchunha();
        int sdt = getPhongTro().getSdt();
        String diachi = getPhongTro().getDiachi();
        int sophong = getPhongTro().getSophong();
        int dientich = getPhongTro().getDientich();
        String ghichu = getPhongTro().getGhichu();
        java.util.Date ngay = getPhongTro().getNgaydang();
        java.sql.Date ngaydang = new java.sql.Date(ngay.getTime());
        int giaphong = getPhongTro().getGiaphong();
        float kinhdo = getPhongTro().getKinhdo();
        float vido = getPhongTro().getVido();
        boolean isActive = getPhongTro().isIsActive();

        int a = 1;
        User mauser = USER_SERVICE.getusersbyID(a);

        int madanhgia = DANHGIA_SERVICE.getMax();

        Danhgia danhgia1 = DANHGIA_SERVICE.getDanhgiabyID(madanhgia);
        System.out.println(madanhgia);

        int quan1 = getPhongTro().getQuan().getMaquan();
        Quan maquan = QUAN_SERVICE.getQuanbyMa(quan1);

        PhongTro phongtro = new PhongTro(0, tieudegioithieu, tenchunha, sdt, diachi, sophong,
                dientich, giaphong, ghichu, ngaydang, kinhdo, vido, true,
                maquan, mauser, danhgia1);
//        if(PHONGTRO_SERVICE.createPhongTro(phongtro)){
//            return "/module/danhsach.jsf?faces-redirect=true";
//        }else{
//             return "/module/themmoi.jsf?faces-redirect=true";
//        }
        if (DANHGIA_SERVICE.createDanhGia(danhgia)) {
            if (PHONGTRO_SERVICE.createPhongTro(phongtro)) {
                return "/module/danhsach.jsf?faces-redirect=true";
            } else {

                return "/module/themmoi.jsf?faces-redirect=true";
            }
        } else {
            return "/module/themmoi.jsf?faces-redirect=true";
        }

//        if (DANHGIA_SERVICE.createDanhGia(danhgia)) {
//            if (PHONGTRO_SERVICE.createPhongTro(phongtro)) {
//                return "/module/danhsach.jsf?faces-redirect=true";
//            } else {
//                return "/module/themmoi.jsf?faces-redirect=true";
//            }
//        } else {
//            return "/module/themmoi.jsf?faces-redirect=true";
//        }
    }

    public String editPhongTro() {
        int maphongtro = getPhongTro().getMaphongtro();
        String tieudegioithieu = getPhongTro().getTieudegioithieu();
        String tenchunha = getPhongTro().getTenchunha();
        int sdt = getPhongTro().getSdt();
        String diachi = getPhongTro().getDiachi();
        int sophong = getPhongTro().getSophong();
        int dientich = getPhongTro().getDientich();
        String ghichu = getPhongTro().getGhichu();
        java.util.Date ngay = getPhongTro().getNgaydang();
        java.sql.Date ngaydang = new java.sql.Date(ngay.getTime());
        int giaphong = getPhongTro().getGiaphong();
        float kinhdo = getPhongTro().getKinhdo();
        float vido = getPhongTro().getVido();
       
        int quan1 = getPhongTro().getQuan().getMaquan();
        Quan maquan = QUAN_SERVICE.getQuanbyMa(quan1);

        PhongTro phongtro = new PhongTro(maphongtro, tieudegioithieu, tenchunha, sdt, diachi, sophong, dientich, giaphong, ghichu, ngaydang, kinhdo, vido, true, maquan,null,null);
        if (PHONGTRO_SERVICE.editPhongTro(phongtro)) {
            return "/module/danhsach.jsf?faces-redirect=true";
        } else {
            return "/module/themmoi.jsf?faces-redirect=true";
        }
    }
}
